package com.qc.controller.web;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qc.entity.Authentication;
import com.qc.entity.SystemMessageEntity;
import com.qc.entity.UserEntity;
import com.qc.jpush.JPushUtil;
import com.qc.service.AuthenticationService;
import com.qc.service.SystemMessageService;
import com.qc.service.UserService;

@Controller
@RequestMapping("/web")
public class AuthenticationController {

	@Resource(name="authenticationService")
	private AuthenticationService authenticationService;
	
	@Resource(name="systemMessageService")
	private SystemMessageService systemMessageService;
	
	@Resource
	private UserService userService;
	
	@RequestMapping(value = "/toAuthenticationIndex", produces = "text/html;charset=UTF-8")
    public ModelAndView toAuthenticationIndex(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView("/sec/sec_inform2");
        
        String userId = request.getParameter("userId");
        
        Authentication authentication = authenticationService.getAuthentication(userId);
        
        mav.addObject("authentication",authentication);
        return mav;
    }
	
	@RequestMapping(value = "/toUpdateAuthentication")
    @ResponseBody
    public int toUpdateAuthentication(HttpServletRequest request) {
		
		String userId = request.getParameter("userId");
		long id = Long.parseLong(request.getParameter("funcid"));
		int type = Integer.parseInt(request.getParameter("type"));
		String describe = request.getParameter("describe");
		
		Authentication authentication = authenticationService.getAuthentication(userId);
		
		authentication.setType(type);
		authentication.setDescribes(describe);
		
		authentication.settId(id);
		
		int flag = authenticationService.updateAuthenticationId(authentication);
		
		SystemMessageEntity messageEntity = new SystemMessageEntity();
		
		if (type == 2) {
			
			messageEntity.setUserId(Long.parseLong(userId));
        	messageEntity.setContent("您好,您的职业技能认证已通过,点击查看!");
        	messageEntity.setSystemType(1);
        	
        	systemMessageService.saveSystemMessage(messageEntity);
			//通过
			JPushUtil.redDotPush(6, Long.parseLong(userId), "您好,您的职业技能认证已通过,点击查看!");
		} else if (type == 3){
			
			messageEntity.setUserId(Long.parseLong(userId));
        	messageEntity.setContent("对不起,由于<"+describe+">,您的职业技能认证未通过,请点击重新提交!");
        	messageEntity.setSystemType(1);
        	
        	systemMessageService.saveSystemMessage(messageEntity);
			//拒绝
			JPushUtil.redDotPush(6, Long.parseLong(userId), "对不起,由于<"+describe+">,您的职业技能认证未通过,请点击重新提交!");
		}
		
		return flag;
	}
	
	
}
